#include "db.h"
#include <QDebug>
#include <QMessageBox>
#include <QMutex>
#include <QMutexLocker>
#include <QApplication>
#include <QTextCodec>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlError>
#include <QSqlRecord>

db::db(QObject *parent, QString name):QObject(parent)
{
    initialized = false;
    init(name);
}

db::~db()
{

}

QSqlDatabase m_db;
void db::init(QString name)
{
    qDebug("init 被调用");

    if(!initialized)
    {
        QSqlDatabase my_db = QSqlDatabase::addDatabase("QSQLITE");
        my_db.setDatabaseName(name);
        if (!my_db.open()) {
            QMessageBox::critical(0, tr("Open Database error"),
                                  tr("Unable to connect database\n"), QMessageBox::Cancel);
            return;
        }

        q = new QSqlQuery(m_db);

        if (name == "COUNTRY") {

            QStringList tables = my_db.tables(QSql::Tables);

            if (!tables.contains("country") && !q->exec("create table country (id int primary key, "
                        "abb text, name_zh text, name_en text, team_size int, medal_db text)"))
            {
                qDebug() << "create table " <<  name << " fail";
            }
        }

        initialized = true;
        return;
    }

    QSqlQuery query;
    QString sql = QString("create table %1 (id int primary key, " 
            "name text, number int, sport text, score int, medal_level text, date text)").arg(name);
    if (!query.exec(sql))
    {
        qDebug() << "create table " <<  name << " fail";
    }
}
void db::dbConnection()
{
    qDebug() << "dbConnection() 被调用";
    //设置本地编码 头文件 #include <QTextCodec> //编码
    QTextCodec *codec = QTextCodec::codecForLocale();
    QTextCodec::setCodecForLocale(codec);


    //打印数据库的驱动的模块
    qDebug()<<":::::::::驱动::::::::::"<<QSqlDatabase::drivers();

    QSqlDatabase m_db;
    if (QSqlDatabase::contains("dh_sql_default_connection"))
    {
        m_db = QSqlDatabase::database("dh_sql_default_connection");

        qDebug() << "contains";
    }
    else
    {
        m_db = QSqlDatabase::addDatabase(
            "QSQLITE", "dh_sql_default_connection");

        m_db.setDatabaseName("m_db.db");
    }

    if(m_db.open()==true)
    {
        QSqlQuery query(m_db);


        QString tableName = "country";


        bool isTableExist = m_db.tables().contains(tableName);


        if(!isTableExist)
        {
            // create table
            bool ret = query.exec(QString("create table %1(numCode int ,alphaCode varchar(255),chiName varchar(255),teamSize int,engName varchar(255),gold int,silver int,bronze int)").arg(tableName));
            qDebug() << "create table ret" << ret << "table does not exist";


            //initTable1();   //初始化添加三条数据

        }
        else
        {
            qDebug() << "table exists";

        }
    }
}

bool db::query(QString sql)
{
    if (initialized) {
        return q->exec(sql);
    }

    return false;
}
